class CustomizationsController < ApplicationController
  
  layout 'application'
    
    def index
            
      
    end
    
    def best_fit #displays the "Best Fit" form 
      #@customization = Customization.new
    end
    
    def best_fit_create #processes the "Best Fit" form
       @customization = Customization.new(params[:customization])
       name = @customization.name.downcase
       model = @customization.model.upcase
       @image = name <<  "_" << model << ".png"
       @customizations = Customization.search(@customization.name.downcase, @customization.model.upcase) 
       
         render(:template => "customizations/best_fit")
      
    end
    
    def browse #displays all the servers on browse page
      @customizations = Customization.all
      
        respond_to do |format|
          format.html # browse.html.erb
          format.json { render :json => @customizations.image }
        end
    end
    
    def browse_create #process the server image selected from browse page
        @customization = Customization.find(params[:id]) #in browse.html.erb the :id MUST be assigned to f.id inside inner form
        render(:template => "customizations/edit")
    end
    
    def edit
      @customization = Customization.find(params[:id]) #this MUST be here so that the :id of the customization 
                                                       # determined via Best Fit or Browse can be displayed to user 
    end
    
    def edit_update
      @customization = Customization.find(params[:id])
     
            if @customization.update_attributes(params[:customization])
              render(:template => "customizations/edit")
            else
              render(:text => "No Update Occured")
            end
    end
    
    def save_appearance
      @new_image = Customization.find(params[:id])
        @new_image.save
        @customization = @new_image
        render(:action => "index", :id => @customization.id)
    end
    
    def image_new
      upload(params)
      
    end
       
    private
     
         def upload(params)
           if (params[:image])
               uploaded_io = params[:image]
                 # MUST write uploaded files to the dir path 'app/assets/images' in order to display
                 # for some reason images that are in public/uploads and NOT in assets/images will NOT display
               File.open(Rails.root.join('app','assets', 'images', uploaded_io.original_filename), 'wb') do |file|
                  file.write(uploaded_io.read)
               end
               
               @customizations = Customization.search_image(uploaded_io.original_filename)
               if(@customizations.empty?)
                 @customization = Customization.create(:image => uploaded_io.original_filename)
               else
                 @customization = @customizations[0] 
               
               end
               render(:template => "customizations/edit")
           else
              render(:template => "customizations/edit")  
           end
         end
      
        def logging
            @customizations = Customization.all
            ActiveSupport::Deprecation.warn("This is a Deprecation")
            logger.debug("This is Debug")
            logger.info("This is info")
            logger.warn("This is warn")
            logger.error("This is error")
            logger.fatal("This is fatal")
            render(:text => "Logged!")
         end
 
end
